/*
 * MPR121.h
 *
 *  Created on: Mar 8, 2014
 *      Author: Misael Munoz
 */

#ifndef MPR121_H_
#define MPR121_H_

/****************************************************************************
** Header Files (Only those that are needed in this file)
****************************************************************************/

/* System Header Files */
#include "common.h"

/* Foreign headerfiles */

/* Own headerfiles */
#include "MPR121_cfg.h"

/***************************************************************************
* Definition of exported global data used by Class
* Macros, Enumerations, Types,  Forward declarations
***************************************************************************/

/**
 * Description: Definition of the Register Address 
 */
#define  ELE07TS              0x00
#define  ELE811TS             0x01
#define  ELE07OOR             0x02
#define  ELE811OOR            0x03
#define  ELE0_EFD_LSB         0x04
#define  ELE0_EFD_MSB         0x05
#define  ELE1_EFD_LSB         0x06
#define  ELE1_EFD_MSB         0x07
#define  ELE2_EFD_LSB         0x08
#define  ELE2_EFD_MSB         0x09
#define  ELE3_EFD_LSB         0x0A
#define  ELE3_EFD_MSB         0x0B
#define  ELE4_EFD_LSB         0x0C
#define  ELE4_EFD_MSB         0x0D
#define  ELE5_EFD_LSB         0x0E
#define  ELE5_EFD_MSB         0x0F
#define  ELE6_EFD_LSB         0x10
#define  ELE6_EFD_MSB         0x11
#define  ELE7_EFD_LSB         0x12
#define  ELE7_EFD_MSB         0x13
#define  ELE8_EFD_LSB         0x14
#define  ELE8_EFD_MSB         0x15
#define  ELE9_EFD_LSB         0x16
#define  ELE9_EFD_MSB         0x17
#define  ELE10_EFD_LSB        0x18
#define  ELE10_EFD_MSB        0x19
#define  ELE11_EFD_LSB        0x1A
#define  ELE11_EFD_MSB        0x1B
#define  ELEPROX_EFD_LSB      0x1C
#define  ELEPROX_EFD_MSB      0x1D
#define  ELE0_BV              0x1E
#define  ELE1_BV              0x1F
#define  ELE2_BV              0x20
#define  ELE3_BV              0x21
#define  ELE4_BV              0x22
#define  ELE5_BV              0x23
#define  ELE6_BV              0x24
#define  ELE7_BV              0x25
#define  ELE8_BV              0x26
#define  ELE9_BV              0x27
#define  ELE10_BV             0x28
#define  ELE11_BV             0x29
#define  ELEPROX_BV           0x2A
#define  MHD_RIS              0x2B
#define  NHD_A_RIS            0x2C
#define  NCL_RIS              0x2D
#define  FDL_RIS              0x2E
#define  MHD_FALL             0x2F
#define  NHD_A_FALL           0x30
#define  NCL_FALL             0x31
#define  FDL_FALL             0x32
#define  NHD_A_TOUCH          0x33
#define  NCL_TOUCHED          0x34
#define  FDL_TOUCHED          0x35
#define  ELEPROX_MHD_R        0x36
#define  ELEPROX_NHD_AR       0x37
#define  ELEPROX_NCL_R        0x38
#define  ELEPROX_FDL_R        0x39
#define  ELEPROX_MHD_F        0x3A
#define  ELEPROX_NHD_AF       0x3B
#define  ELEPROX_NCL_F        0x3C
#define  ELEPROX_FDL_F        0x3D
#define  ELEPROX_NHD_AT       0x3E
#define  ELEPROX_NCL_T        0x3F
#define  ELEPROX_FDL_T        0x40
#define  ELE0_TT              0x41
#define  ELE0_RT              0x42
#define  ELE1_TT              0x43
#define  ELE1_RT              0x44
#define  ELE2_TT              0x45
#define  ELE2_RT              0x46
#define  ELE3_TT              0x47
#define  ELE3_RT              0x48
#define  ELE4_TT              0x49
#define  ELE4_RT              0x4A
#define  ELE5_TT              0x4B
#define  ELE5_RT              0x4C
#define  ELE6_TT              0x4D
#define  ELE6_RT              0x4E
#define  ELE7_TT              0x4F
#define  ELE7_RT              0x50
#define  ELE8_TT              0x51
#define  ELE8_RT              0x52
#define  ELE9_TT              0x53
#define  ELE9_RT              0x54
#define  ELE10_TT             0x55
#define  ELE10_RT             0x56
#define  ELE11_TT             0x57
#define  ELE11_RT             0x58
#define  ELEPROX_TT           0x59
#define  ELEPROX_RT           0x5A
#define  DEBOUNCE_TOUCH_RELE  0x5B
#define  AFE_CONFIG           0x5C
#define  FILTER_CONFIG        0x5D
#define  ELECTR_CONFIG        0x5E
#define  ELE0_EC              0x5F
#define  ELE1_EC              0x60
#define  ELE2_EC              0x61
#define  ELE3_EC              0x62
#define  ELE4_EC              0x63
#define  ELE5_EC              0x64
#define  ELE6_EC              0x65
#define  ELE7_EC              0x66
#define  ELE8_EC              0x67
#define  ELE9_EC              0x68
#define  ELE10_EC             0x69
#define  ELE11_EC             0x6A
#define  ELEPROX_EC           0x6B
#define  ELE01_CT             0x6C
#define  ELE23_CT             0x6D
#define  ELE45_CT             0x6E
#define  ELE67_CT             0x6F
#define  ELE89_CT             0x70
#define  ELE1011_CT           0x71
#define  ELEPROX_CT           0x72
#define  GPIO_CR0             0x73
#define  GPIO_CR1             0x74
#define  GPIO_DATA_REG        0x75
#define  GPIO_DIR_REG         0x76
#define  GPIO_ENA_REG         0x77
#define  GPIO_DATA_SET_REG    0x78
#define  GPIO_DATA_CLEAR_REG  0x79
#define  GPIO_DATA_TGG_REG    0x7A
#define  AUTO_CR0             0x7B
#define  AUTO_CR1             0x7C
#define  AUTO_USL             0x7D
#define  AUTO_LSL             0x7E
#define  AUTO_TLR             0x7F
#define  SRST                 0x80

/**
 * Description: Definition of the number of bytes
 * in the first write operation  
 */
#define MPR__nInitHWFirsWrite    ((uint8)8u)

/**
 * Description: Definition of the number of bytes
 * in the Second write operation  
 */
#define MPR__nInitHWSecoWrite    ((uint8)25u)

/**
 * Description: Definition of the number of bytes
 * in the third write operation  
 */
#define MPR__nInitHWThirdWrite   ((uint8)3u)

/**
 * Description: Definition of the number of bytes
 * in the fourth write operation  
 */
#define MPR__nInitHWFourthWrite  ((uint8)5u)

/**
 * Description: The maximum size of the buffer
 * for the a MPR read operation. 
 */
#define MPR__nMaxBuffReg         ((uint8)0x80)

/**
 * Description: The number of bytes of the complete
 * memory map of the MPR device. 
 */
#define MPR__nAllMPRmemory       ((uint8)0x7F)

/**
 * Description: Definition of the polling time
 * in milliseconds 
 */
#define MPR__nPollingTime        200

/**
 * Description: Macro to stock the code until a job has finished 
 */
#define MPR__WaitFinisJob     do                            \
                              {                             \
                              EVHD_vStartDispatcher();      \
                              } while (I2C_bDeviceBusy(I2C_enTouchDev) == 1)

/**
 * Description: Macro to perform the i2c job that commands a Soft
 * reset in the MPR touch driver 
 */  
#define MPR__vSoftReset()     I2C_WriteJob(I2C_enTouchDev, SRST, &MPR__su8aMPRInitConfig[N_SOFT_RST] , 0x01);\
                              MPR__WaitFinisJob;
/**
 * Description: Macro to perform the i2c job that writes the Electrode
 * configuration in the MPR touch driver  
 */
#define MPR_vWriteElectrodeConfig()    I2C_WriteJob(I2C_enTouchDev, ELECTR_CONFIG, &MPR__su8aMPRInitConfig[N_ELECTR_CONFIG] , 0x01);\
                                       MPR__WaitFinisJob;


/**
 * Description: Enum for handling array of initial 
 * hardware parameters. 
 */
enum enu_RegADDR{
   N_MHD_RIS = 0,
   N_NHD_A_RIS,
   N_NCL_RIS,
   N_FDL_RIS,
   N_MHD_FALL,
   N_NHD_A_FALL,
   N_NCL_FALL,
   N_FDL_FALL,
   N_ELE0_TT,
   N_ELE0_RT,
   N_ELE1_TT,
   N_ELE1_RT,
   N_ELE2_TT,
   N_ELE2_RT,
   N_ELE3_TT,
   N_ELE3_RT,
   N_ELE4_TT,
   N_ELE4_RT,
   N_ELE5_TT,
   N_ELE5_RT,
   N_ELE6_TT,
   N_ELE6_RT,
   N_ELE7_TT,
   N_ELE7_RT,
   N_ELE8_TT,
   N_ELE8_RT,
   N_ELE9_TT,
   N_ELE9_RT,
   N_ELE10_TT,
   N_ELE10_RT,
   N_ELE11_TT,
   N_ELE11_RT,
   N_AFE_CONFIG,
   N_FILTER_CONFIG,
   N_ELECTR_CONFIG,
   N_AUTO_CR0,
   N_AUTO_CR1,
   N_AUTO_USL,
   N_AUTO_LSL,
   N_AUTO_TLR,
   N_SOFT_RST
};

/***************************************************************************
* CLASS DEFINITION
***************************************************************************/

typedef struct
{
/***************************************************************************
 *   Instance Attributes
 ***************************************************************************/
   
   /**
   * Software Timer intended for touch pollig    
   * @values 0x00 ... 0xFF
   */
   uint8 u8SwTimerTouchPolling;
} MPR_CInterface;


/***************************************************************************
*   Class Attributes
***************************************************************************/

/* System Wide Scope */


/* File Local Scope */
#ifdef MPR_Interface_C

   /**
   * Values of the current lecture of the MPR12 Registers 
   * @range  
   * @values 0x00 ... 0xFF
   */
   static uint8 MPR__u8aCurrentReg[MPR__nMaxBuffReg] = {0};
   
   /**
   * Values of the past lecture of the MPR12 Registers 
   * @range  
   * @values 0x00 ... 0xFF
   */
   static uint8 MPR__u8aPastReg[MPR__nMaxBuffReg] = {0};
   

#endif

/***************************************************************************
*   Class Operations
***************************************************************************/

/* System Wide Scope */  

   /**
   * MPR hardware initialization function 
   * @return None
   * @author Misael Munoz
   * @param  None
   */
   void MPR_HWInit (void);

   /**
   * MPR Finish job handler function 
   * @return None
   * @author Misael Munoz
   * @param  job: specific job to be handled 
   */
   void MPR__vFinishjobHandler(uint8 job);

#if (MPR121_MODE == MPR121_POLLING_MODE) 
   /**
   * Polling function for read electrode status 
   * of the touch sensor. This function should 
   * be called cyclically 
   * @return None
   * @author Misael Munoz
   * @param  None
   */
   void MPR_vPollingELERead(void);

#elif (MPR121_MODE == MPR121_INTERRUPT_MODE)
   /**
   * Interrupt handler function for read electrode status 
   * The device generates a interrupt when a change in the 
   * electrode status has occur.  
   * @return None
   * @author Misael Munoz
   * @param  None
   */
   void MPR_vInterruptELERead(void);
#else
#error "Unknown configuration variable"
#endif 
/* Note: Package Global Scope Functions should only be contained in private classes */

/* File Local Scope */

#ifdef MPR_Interface_C


#endif



/********************
**  CLASS END
*********************/


#endif /* MPR121_H_ */
